<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.8.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>js-深入-promise模拟实现 | lafen | lafen&#39;s personal blog</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="js,promise">
    <meta name="description" content="promise 模拟实现promise 使用先看如何创建一个 promise 实例 1new Promise((resolve, reject) =&amp;gt; &amp;#123;&amp;#125;)  传入一个函数 （executor） 作为参数，传入即执行 executor 里面有两个参数，一个 resolve， 一个 reject  Promise 类1234567class Promise &amp;#123;">
<meta name="keywords" content="js,promise">
<meta property="og:type" content="article">
<meta property="og:title" content="js-深入-promise模拟实现">
<meta property="og:url" content="https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/index.html">
<meta property="og:site_name" content="lafen">
<meta property="og:description" content="promise 模拟实现promise 使用先看如何创建一个 promise 实例 1new Promise((resolve, reject) =&amp;gt; &amp;#123;&amp;#125;)  传入一个函数 （executor） 作为参数，传入即执行 executor 里面有两个参数，一个 resolve， 一个 reject  Promise 类1234567class Promise &amp;#123;">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-01-10T03:55:24.525Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="js-深入-promise模拟实现">
<meta name="twitter:description" content="promise 模拟实现promise 使用先看如何创建一个 promise 实例 1new Promise((resolve, reject) =&amp;gt; &amp;#123;&amp;#125;)  传入一个函数 （executor） 作为参数，传入即执行 executor 里面有两个参数，一个 resolve， 一个 reject  Promise 类1234567class Promise &amp;#123;">
    
        <link rel="alternate" type="application/atom+xml" title="lafen" href="/atom.xml">
    
    <link rel="shortcut icon" href="/img/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide">
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">lafen</h5>
          <a href="mailto:1031822863@qq.com" title="1031822863@qq.com" class="mail">1031822863@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/">
                <i class="icon icon-lg icon-home"></i>
                home
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives">
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags">
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories">
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/zhongxiaotian" target="_blank">
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="http://www.weibo.com/" target="_blank">
                <i class="icon icon-lg icon-weibo"></i>
                Weibo
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://gitee.com/lafen" target="_blank">
                <i class="icon icon-lg icon-link"></i>
                Gitee
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">js-深入-promise模拟实现</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">js-深入-promise模拟实现</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-11-26T07:23:56.000Z" itemprop="datePublished" class="page-time">
  2019-11-26
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/js/">js</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/js/promise/">promise</a></li></ul></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#promise-模拟实现"><span class="post-toc-number">1.</span> <span class="post-toc-text">promise 模拟实现</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#promise-使用"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">promise 使用</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Promise-类"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">Promise 类</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Promise-基本状态"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">Promise 基本状态</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#Promise-then-方法"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">Promise.then 方法</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#尝试使用-Promise"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">尝试使用 Promise</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#解决异步调用问题"><span class="post-toc-number">1.6.</span> <span class="post-toc-text">解决异步调用问题</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#链式调用"><span class="post-toc-number">1.7.</span> <span class="post-toc-text">链式调用</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#其他问题"><span class="post-toc-number">1.8.</span> <span class="post-toc-text">其他问题</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#参考"><span class="post-toc-number">1.9.</span> <span class="post-toc-text">参考</span></a></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-js-深入-promise模拟实现/js-深入-promise模拟实现" class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">js-深入-promise模拟实现</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-11-26 15:23:56" datetime="2019-11-26T07:23:56.000Z" itemprop="datePublished">2019-11-26</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/js/">js</a><ul class="article-category-list-child"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/js/promise/">promise</a></li></ul></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style="display:none">
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h4 id="promise-模拟实现"><a href="#promise-模拟实现" class="headerlink" title="promise 模拟实现"></a>promise 模拟实现</h4><h5 id="promise-使用"><a href="#promise-使用" class="headerlink" title="promise 使用"></a>promise 使用</h5><p>先看如何创建一个 <code>promise</code> 实例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;&#125;)</span><br></pre></td></tr></table></figure>
<ul>
<li>传入一个函数 （<code>executor</code>） 作为参数，传入即执行</li>
<li><code>executor</code> 里面有两个参数，一个 <code>resolve</code>， 一个 <code>reject</code></li>
</ul>
<h5 id="Promise-类"><a href="#Promise-类" class="headerlink" title="Promise 类"></a>Promise 类</h5><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="keyword">let</span> resolve = <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;</span><br><span class="line">        <span class="keyword">let</span> reject = <span class="function"><span class="params">()</span> =&gt;</span> &#123;&#125;</span><br><span class="line">        executor(resolve, reject)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="Promise-基本状态"><a href="#Promise-基本状态" class="headerlink" title="Promise 基本状态"></a>Promise 基本状态</h5><p><code>promise</code> 有三种基本状态，其中的状态流转如下</p>
<ul>
<li><code>state</code> 有 <code>pending</code>、<code>fulfilled</code>、<code>rejected</code></li>
<li><code>pending</code> –&gt; <code>fulfilled</code> 不可逆，且有一个不可改变的 <code>value</code> 值</li>
<li><code>pending</code> –&gt; <code>rejected</code> 不可逆，且有一个不可改变的 <code>reason</code> 值</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="comment">// 构造器</span></span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="keyword">this</span>.state = <span class="string">'pending'</span> <span class="comment">// 状态</span></span><br><span class="line">        <span class="keyword">this</span>.value = <span class="literal">undefined</span> <span class="comment">// 成功值</span></span><br><span class="line">        <span class="keyword">this</span>.reason = <span class="literal">undefined</span> <span class="comment">// 失败值</span></span><br><span class="line">        <span class="keyword">let</span> resolve = <span class="function"><span class="params">value</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">                <span class="comment">// resolve 调用后，state 转化为成功状态</span></span><br><span class="line">            	<span class="keyword">this</span>.state = <span class="string">'fulfilled'</span></span><br><span class="line">                <span class="comment">// 储存成功的值</span></span><br><span class="line">                <span class="keyword">this</span>.value = value</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> reject = <span class="function"><span class="params">reason</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">            	<span class="keyword">this</span>.state = <span class="string">'rejected'</span></span><br><span class="line">                <span class="keyword">this</span>.reason = reason</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 立即执行传入的函数</span></span><br><span class="line">            executor(resolve, reject)</span><br><span class="line">        &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">            <span class="comment">// 报错则直接拒绝</span></span><br><span class="line">            reject(err)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="Promise-then-方法"><a href="#Promise-then-方法" class="headerlink" title="Promise.then 方法"></a>Promise.then 方法</h5><p><code>then</code> 方法中有两个参数 <code>onFulfilled</code>, <code>onRejected</code>，他们也是函数的话则根据<strong>状态</strong>来执行</p>
<ul>
<li><code>state = &#39;fulfilled&#39;</code> ，则执行 <code>onFulfilled</code>，传入 <code>this.value</code> </li>
<li><code>state = &#39;rejected&#39;</code> ，则执行 <code>onRejected</code>， 传入 <code>this.reason</code></li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123; <span class="comment">/**code...*/</span> &#125;</span><br><span class="line">    <span class="comment">// then 方法</span></span><br><span class="line">    then(onFulfilled, onRejected) &#123;</span><br><span class="line">        <span class="comment">// 成功</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'fulfilled'</span>) &#123;</span><br><span class="line">        	onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 失败</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'rejected'</span>) &#123;</span><br><span class="line">           onRejected(<span class="keyword">this</span>.reason)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="尝试使用-Promise"><a href="#尝试使用-Promise" class="headerlink" title="尝试使用 Promise"></a>尝试使用 Promise</h5><p>至此，貌似 <code>Promise</code> 可以用起来了，尝试一下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">'1. 立即执行'</span>) <span class="comment">// 立即执行</span></span><br><span class="line">    resolve(<span class="string">'2. 成功'</span>) <span class="comment">// 在then方法中返回结果</span></span><br><span class="line">	<span class="comment">// reject('2. 失败')</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 调用then方法，获得结果值或失败原因</span></span><br><span class="line">p.then(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(value) <span class="comment">// 成功的方法</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">reason</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(reason) <span class="comment">// 失败的方法</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 1. 立即执行</span></span><br><span class="line"><span class="comment">// 2. 成功</span></span><br></pre></td></tr></table></figure>
<p>经过尝试可见，貌似 <code>Promise</code> 正常运行起来了，但是有问题，就是 <code>Promise</code> 中的 <code>resolve</code> 在异步中执行的情况，会导致我们调用 <code>then</code> 方法的时候，<code>state</code> 还是 <code>pending</code> 的等待状态。</p>
<h5 id="解决异步调用问题"><a href="#解决异步调用问题" class="headerlink" title="解决异步调用问题"></a>解决异步调用问题</h5><p>我们需要在 then 调用的时候，将成功和失败储存到各自的执行队列中，一旦状态改变，就调用他们</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="comment">// code...</span></span><br><span class="line">        <span class="keyword">this</span>.onResolvedCallbacks = [] <span class="comment">// 成功存放回调的数组</span></span><br><span class="line">        <span class="keyword">this</span>.onRejectedCallbacks = [] <span class="comment">// 失败存放回调的数组</span></span><br><span class="line">        <span class="keyword">let</span> resolve = <span class="function"><span class="params">value</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.sate === <span class="string">'pending'</span>) &#123;</span><br><span class="line">                <span class="comment">// ...</span></span><br><span class="line">                <span class="comment">// resolve 之后调用成功数组里的函数</span></span><br><span class="line">                <span class="keyword">this</span>.onResolvedCallbacks.forEach(<span class="function"><span class="params">fn</span> =&gt;</span> fn())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> reject = <span class="function"><span class="params">value</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.sate === <span class="string">'pending'</span>) &#123;</span><br><span class="line">                <span class="comment">// ...</span></span><br><span class="line">                <span class="comment">// reject 之后调用失败数组里的函数</span></span><br><span class="line">                <span class="keyword">this</span>.onRejectedCallbacks.forEach(<span class="function"><span class="params">fn</span> =&gt;</span> fn())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// code...</span></span><br><span class="line">    &#125;</span><br><span class="line">    then(onFulfilled, onRejected) &#123;</span><br><span class="line">        <span class="comment">// code...</span></span><br><span class="line">        <span class="comment">// 当状态还是 pending</span></span><br><span class="line">        <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">            <span class="comment">// push 进成功的数组</span></span><br><span class="line">            <span class="keyword">this</span>.onResolvedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">            &#125;)</span><br><span class="line">            <span class="comment">// push 进失败的数组</span></span><br><span class="line">            <span class="keyword">this</span>.onRejectedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                onRejected(<span class="keyword">this</span>.reason)</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>此时我们又可以进行尝试一下，我们的 <code>Promise</code> 是否支持了异步，将之前的例子改一下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> p = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="keyword">function</span>(<span class="params">resolve, reject</span>) </span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(<span class="string">'1. 立即执行'</span>) <span class="comment">// 立即执行</span></span><br><span class="line">    setTimeout(<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="comment">// 在then方法中返回结果</span></span><br><span class="line">        resolve(<span class="string">'2. 成功'</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">	<span class="comment">// reject('2. 失败')</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 调用then方法，获得结果值或失败原因</span></span><br><span class="line">p.then(<span class="function"><span class="keyword">function</span>(<span class="params">value</span>)</span>&#123;</span><br><span class="line">	<span class="built_in">console</span>.log(value) <span class="comment">// 成功的方法</span></span><br><span class="line">&#125;, <span class="function"><span class="keyword">function</span>(<span class="params">reason</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(reason) <span class="comment">// 失败的方法</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 1. 立即执行</span></span><br><span class="line"><span class="comment">// ...1s后</span></span><br><span class="line"><span class="comment">// 2. 成功</span></span><br></pre></td></tr></table></figure>
<p>可见异步问题已经解决了，<code>nice</code> 啊</p>
<h5 id="链式调用"><a href="#链式调用" class="headerlink" title="链式调用"></a>链式调用</h5><p>链式调用，即 <code>new Promise().then().then()</code></p>
<ul>
<li>为了形成链式调用，<code>Promise</code> 规定就是在 <code>then</code> 方法里面返回一个<strong>新的</strong> <code>Promise</code> ，即 <code>Promise2 = new Promise((resolve, reject) =&gt; {})</code>。</li>
<li>当在第一个 <code>then</code> 中 <code>return</code> 了一个<strong>参数</strong>（需判断），这个 <code>return</code> 出来的新的 <code>promise</code> 就是 <code>onFulfilled()</code> 或 <code>onRejected()</code> 的值</li>
</ul>
<p><code>promise</code>规范规定<code>onFulfilled()</code> 或 <code>onRejected()</code> 的值，即第一个 <code>then</code> 返回的值，叫做 <code>x</code>，判断 <code>x</code> 的函数叫做 <code>resolvePromise</code></p>
<ul>
<li><code>x === promise</code> ？  如果是，则取他的结果，作为新的 <code>promise2</code> 成功的结果</li>
<li><code>x === 普通值</code>？ 直接作为 <code>promise2</code> 成功的结果</li>
</ul>
<p><code>resolvePromise</code> 的参数有 <code>promise2</code>（默认返回的 <code>promise</code>）、<code>x</code>（我们自己 <code>return</code> 的对象）、<code>resolve</code>、<code>reject</code>，<code>resolve</code>、<code>reject</code>是 <code>promise2</code> 的</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="comment">// ...code...    </span></span><br><span class="line">    &#125;</span><br><span class="line">    then(onFulfilled, onRejected) &#123;</span><br><span class="line">        <span class="comment">// 声明返回的 promise2</span></span><br><span class="line">        <span class="keyword">let</span> promise2 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'fulfilled'</span>) &#123;</span><br><span class="line">               <span class="keyword">let</span> x = onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">               <span class="comment">// 处理自己 return 的promise 和默认的 promise2 的关系</span></span><br><span class="line">               resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'rejected'</span>) &#123;</span><br><span class="line">               <span class="keyword">let</span> x = onRejected(<span class="keyword">this</span>.reason)</span><br><span class="line">               resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">               <span class="keyword">this</span>.onResolvedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                   <span class="keyword">let</span> x = onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">                   resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">               &#125;)</span><br><span class="line">               <span class="keyword">this</span>.onRejectedCallbacks.push(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">       				<span class="keyword">let</span> x = onRejected(<span class="keyword">this</span>.reason);</span><br><span class="line">          			resolvePromise(promise2, x, resolve, reject);</span><br><span class="line">        		&#125;)</span><br><span class="line">           	&#125;</span><br><span class="line">        &#125;)</span><br><span class="line">        <span class="comment">// 返回 promise2，形成链式</span></span><br><span class="line">        <span class="keyword">return</span> promise2</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p> <strong>promise规范</strong>规定了一段代码，让不同的<code>promise</code>代码互相套用，叫做<code>resolvePromise</code> </p>
<p>如果 <code>x === promise2</code>，则会造成循环引用，自己等待自己完成，则报<strong>循环引用</strong>错误</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> p = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">  resolve(<span class="number">0</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="keyword">var</span> p2 = p.then(<span class="function"><span class="params">data</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 循环引用，自己等待自己完成，则一直都完不成</span></span><br><span class="line">  <span class="keyword">return</span> p2;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>所以需要判断 <code>x</code></p>
<ul>
<li>x 不能是 null</li>
<li>x 是普通值，直接 resolve(x)</li>
<li>x 是对象或者函数（包括promise），let then = x.then</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">resolvePromise</span>(<span class="params">promise2, x, resolve, reject</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 循环引用报错</span></span><br><span class="line">  <span class="keyword">if</span>(x === promise2)&#123;</span><br><span class="line">    <span class="comment">// reject报错</span></span><br><span class="line">    <span class="keyword">return</span> reject(<span class="keyword">new</span> <span class="built_in">TypeError</span>(<span class="string">'Chaining cycle detected for promise'</span>));</span><br><span class="line"> &#125;</span><br><span class="line">  <span class="comment">// 防止多次调用</span></span><br><span class="line"> <span class="keyword">let</span> called;</span><br><span class="line">  <span class="comment">// x不是null 且x是对象或者函数</span></span><br><span class="line">  <span class="keyword">if</span> (x != <span class="literal">null</span> &amp;&amp; (<span class="keyword">typeof</span> x === <span class="string">'object'</span> || <span class="keyword">typeof</span> x === <span class="string">'function'</span>)) &#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">// A+规定，声明then = x的then方法</span></span><br><span class="line">      <span class="keyword">let</span> then = x.then;</span><br><span class="line">      <span class="comment">// 如果then是函数，就默认是promise了</span></span><br><span class="line">      <span class="keyword">if</span> (<span class="keyword">typeof</span> then === <span class="string">'function'</span>) &#123; </span><br><span class="line">        <span class="comment">// 就让then执行 第一个参数是this   后面是成功的回调 和 失败的回调</span></span><br><span class="line">        then.call(x, y =&gt; &#123;</span><br><span class="line">          <span class="comment">// 成功和失败只能调用一个</span></span><br><span class="line">          <span class="keyword">if</span> (called) <span class="keyword">return</span>;</span><br><span class="line">          called = <span class="literal">true</span>;</span><br><span class="line">          <span class="comment">// resolve的结果依旧是promise 那就继续解析</span></span><br><span class="line">          resolvePromise(promise2, y, resolve, reject);</span><br><span class="line">        &#125;, err =&gt; &#123;</span><br><span class="line">          <span class="comment">// 成功和失败只能调用一个</span></span><br><span class="line">          <span class="keyword">if</span> (called) <span class="keyword">return</span>;</span><br><span class="line">          called = <span class="literal">true</span>;</span><br><span class="line">          reject(err);<span class="comment">// 失败了就失败了</span></span><br><span class="line">        &#125;)</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        resolve(x); <span class="comment">// 直接成功即可</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">      <span class="comment">// 也属于失败</span></span><br><span class="line">      <span class="keyword">if</span> (called) <span class="keyword">return</span>;</span><br><span class="line">      called = <span class="literal">true</span>;</span><br><span class="line">      <span class="comment">// 取then出错了那就不要在继续执行了</span></span><br><span class="line">      reject(e); </span><br><span class="line">    &#125;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    resolve(x);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="其他问题"><a href="#其他问题" class="headerlink" title="其他问题"></a>其他问题</h5><p><code>promise</code>规定 <code>onFulfilled</code>、<code>onRejected</code>都是可选参数，如果他们不是函数，必须被忽略</p>
<p> <code>onRejected</code>返回一个<strong>普通值</strong>，失败时如果直接等于 <code>value =&gt; value</code>，则会跑到下一个<code>then</code>中的<code>onFulfilled</code>中，所以直接扔出一个错误 <code>reason =&gt; throw err</code> </p>
<p> <code>promise</code>规定<code>onFulfilled</code>或<code>onRejected</code>不能同步被调用，必须<strong>异步调用</strong>。我们就用<code>setTimeout</code>解决异步问题 </p>
<p> 如果<code>onFulfilled</code>或<code>onRejected</code>报错，则直接返回<code>reject()</code> </p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Promise</span> </span>&#123;</span><br><span class="line">    <span class="comment">// ...code...</span></span><br><span class="line">    <span class="comment">// 构造器</span></span><br><span class="line">    <span class="keyword">constructor</span>(executor) &#123;</span><br><span class="line">        <span class="keyword">this</span>.state = <span class="string">'pending'</span> <span class="comment">// 状态</span></span><br><span class="line">        <span class="keyword">this</span>.value = <span class="literal">undefined</span> <span class="comment">// 成功值</span></span><br><span class="line">        <span class="keyword">this</span>.reason = <span class="literal">undefined</span> <span class="comment">// 失败值</span></span><br><span class="line">        <span class="keyword">this</span>.onResolvedCallbacks = []</span><br><span class="line">    	<span class="keyword">this</span>.onRejectedCallbacks = []</span><br><span class="line">        <span class="keyword">let</span> resolve = <span class="function"><span class="params">value</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">                <span class="comment">// resolve 调用后，state 转化为成功状态</span></span><br><span class="line">            	<span class="keyword">this</span>.state = <span class="string">'fulfilled'</span></span><br><span class="line">                <span class="comment">// 储存成功的值</span></span><br><span class="line">                <span class="keyword">this</span>.value = value</span><br><span class="line">                <span class="keyword">this</span>.onResolvedCallbacks.forEach(<span class="function"><span class="params">fn</span>=&gt;</span>fn())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> reject = <span class="function"><span class="params">reason</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">            	<span class="keyword">this</span>.state = <span class="string">'rejected'</span></span><br><span class="line">                <span class="keyword">this</span>.reason = reason</span><br><span class="line">                <span class="keyword">this</span>.onRejectedCallbacks.forEach(<span class="function"><span class="params">fn</span>=&gt;</span>fn())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="comment">// 立即执行传入的函数</span></span><br><span class="line">            executor(resolve, reject)</span><br><span class="line">        &#125; <span class="keyword">catch</span> (err) &#123;</span><br><span class="line">            <span class="comment">// 报错则直接拒绝</span></span><br><span class="line">            reject(err)</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// then 方法</span></span><br><span class="line">    then(onFulfilled, onRejected) &#123;</span><br><span class="line">        <span class="comment">// onfulfilled 如果不是函数，就忽略 onFulfilled，直接返回 value</span></span><br><span class="line">        onFulfilled = <span class="keyword">typeof</span> onFulfilled === <span class="string">'function'</span> ? onFulfilled : <span class="function"><span class="params">value</span> =&gt;</span> value</span><br><span class="line">        <span class="comment">// onRejeted 如果不是函数，就忽略 onRejected，直接扔出错误</span></span><br><span class="line">        onRejected = <span class="keyword">typeof</span> onRejected === <span class="string">'function'</span> ? <span class="string">'onRejected'</span> : <span class="function"><span class="params">err</span> =&gt;</span> &#123; <span class="keyword">throw</span> err &#125;</span><br><span class="line">        <span class="keyword">let</span> promise1 = <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function">(<span class="params">resolve, reject</span>) =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'fulfilled'</span>) &#123;</span><br><span class="line">               setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                   <span class="keyword">try</span> &#123;</span><br><span class="line">                       <span class="keyword">let</span> x = onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">                       resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">                   &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">                       reject(e)</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;, <span class="number">0</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'rejected'</span>) &#123;</span><br><span class="line">               setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                   <span class="keyword">try</span> &#123;</span><br><span class="line">                       <span class="keyword">let</span> x = onRejected(<span class="keyword">this</span>.reason)</span><br><span class="line">                       resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">                   &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">                       reject(e)</span><br><span class="line">                   &#125;</span><br><span class="line">               &#125;, <span class="number">0</span>)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span>(<span class="keyword">this</span>.state === <span class="string">'pending'</span>) &#123;</span><br><span class="line">               	<span class="keyword">this</span>.onResolvedCallbacks.push(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                   setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                       <span class="keyword">try</span> &#123;</span><br><span class="line">                           <span class="keyword">let</span> x = onFulfilled(<span class="keyword">this</span>.value)</span><br><span class="line">                   		   resolvePromise(promise2, x, resolve, reject)</span><br><span class="line">                       &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">                           reject(e)</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;, <span class="number">0</span>)</span><br><span class="line">               &#125;)</span><br><span class="line">               <span class="keyword">this</span>.onRejectedCallbacks.push(<span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">                   setTimeout(<span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">                       <span class="keyword">try</span> &#123;</span><br><span class="line">                           <span class="keyword">let</span> x = onRejected(<span class="keyword">this</span>.reason);</span><br><span class="line">          					resolvePromise(promise2, x, resolve, reject);</span><br><span class="line">                       &#125; <span class="keyword">catch</span>(e) &#123;</span><br><span class="line">                           reject(e)</span><br><span class="line">                       &#125;</span><br><span class="line">                   &#125;, <span class="number">0</span>)</span><br><span class="line">        		&#125;)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h5><p><a href="https://yq.aliyun.com/articles/613412" target="_blank" rel="noopener">BAT前端经典面试问题：史上最最最详细的手写Promise教程</a></p>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2020-01-10T03:55:24.525Z" itemprop="dateUpdated">2020-01-10 11:55:24</time>
</span><br>


        
        关于学习类文章网上已经有非常多，而此处文章都是我在学习过程中整理摘录的笔记，参考文章可以点击文末的参考链接进行查阅
        
    </div>
    
    <footer>
        <a href="https://lafen.gitee.io">
            <img src="/img/avatar.jpg" alt="lafen">
            lafen
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/js/">js</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/promise/">promise</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&title=《js-深入-promise模拟实现》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&title=《js-深入-promise模拟实现》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《js-深入-promise模拟实现》 — lafen&url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/11/30/vue-源码笔记-初始化方法分析/vue-源码笔记-初始化方法分析/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">vue-源码笔记-初始化方法分析</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/11/23/js-深入-new模拟实现/js-深入-new模拟实现/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">js-深入-new模拟实现</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        打笔巨款？~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check" data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style="display:none">
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style="display:none">
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>lafen &copy; 2019 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&title=《js-深入-promise模拟实现》 — lafen&pic=https://lafen.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&title=《js-深入-promise模拟实现》 — lafen&source=lafen's personal blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《js-深入-promise模拟实现》 — lafen&url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/&via=https://lafen.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lafen.gitee.io/2019/11/26/js-深入-promise模拟实现/js-深入-promise模拟实现/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"positon":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.8},"react":{"opacity":0.8,"opacityDefault":0.8,"opacityOnHover":0.6},"log":false});</script></body>
</html>
